package com.imti.servicedemo;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.imti.servicedemo.MyService.MyBinder;

public class MainActivity extends AppCompatActivity
{

    private static final String TAG = MainActivity.class.getSimpleName();

    private ServiceConnection mConn;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView()
    {
        Button btnStart = findViewById(R.id.btn_start_service);
        Button btnStop = findViewById(R.id.btn_stop_service);

        Button btnBind = findViewById(R.id.btn_bind_service);
        Button btnUnbind = findViewById(R.id.btn_unbind_service);

        btnStart.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                //开启服务
                startService(new Intent(
                    MainActivity.this, MyService.class));
            }
        });

        btnStop.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                //停止服务
                stopService(new Intent(
                    MainActivity.this, MyService.class));
            }
        });

        btnBind.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                mConn = new ServiceConnection()
                {
                    //绑定成功
                    @Override
                    public void onServiceConnected(ComponentName name, IBinder service)
                    {
                        String text = ((MyBinder) service).getStr();

                        ((MyBinder) service).next();

                        Log.d(TAG, "连接成功后调用Service返回的数据：" + text);
                    }
                    //解绑成功
                    @Override
                    public void onServiceDisconnected(ComponentName name)
                    {

                    }
                };

                //绑定服务
                //参数1：Intent负责绑定启动指定服务
                //参数2：绑定Service后的回调方法
                //参数3：BIND_AUTO_CREATE 标识为服务如果未启动，则启动服务
                bindService(new Intent(
                        MainActivity.this, MyService.class),
                    mConn,
                    BIND_AUTO_CREATE);
            }
        });

        btnUnbind.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (null != mConn)
                {
                    //解绑服务
                    unbindService(mConn);
                }
                //释放ServiceConnection
                mConn = null;
            }
        });
    }
}